package com.itita.ww2.data;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.tuple.Pair;

import com.smartfoxserver.v2.entities.data.ISFSArray;
import com.smartfoxserver.v2.entities.data.ISFSObject;

public class ChestRuleConfig {
	public static boolean isLoaded;

	private static ISFSObject chest_rule; // 宝箱抽取规则

	public static final void init(ISFSObject data) {
		chest_rule = data.getSFSObject("chest_rule");
		isLoaded = true;
	}

	private static final ISFSObject getRuleObj(String id) {
		return chest_rule.getSFSObject(id);
	}

	public static enum RuleType {
		UNKNOWN, // 0置位
		FIXED, // 固定宝箱
		RANDOM, // 随机
		FIXED_RANDOM, // 固定+随机
	}

	public static final RuleType getRuleType(String id) {
		return RuleType.values()[getRuleObj(id).getInt("type")];
	}

	public static enum ContentType {
		UNKNOWN, // 0置位
		ITEM, // 物品
		ARMY, // 兵种
	}

	public static final ContentType getContentType(String id) {
		return ContentType.values()[getRuleObj(id).getInt("content_type")];
	}

	public static final int getParam(String id) {
		return getRuleObj(id).getInt("param");
	}

	public static final ISFSObject getFixedList(String id) {
		return getRuleObj(id).getSFSObject("fixedList");
	}

	public static final ISFSObject getRandomList(String id) {
		return getRuleObj(id).getSFSObject("randomList");
	}

	public static final List<Pair<String, Number>> getRandomListAsPairList(String id) {
		List<Pair<String, Number>> ref = new ArrayList<>();
		ISFSObject obj = getRandomList(id);
		for (String key : obj.getKeys()) {
			ref.add(Pair.of(key, obj.getInt(key)));
		}
		return ref;
	}

	public static enum RandomType {
		UNKNOWN, // 0
		ONLYFIRST, // 随机一次叠加数量
		EVERYTIMES, // 每次均随机 
	}

	public static final RandomType getRandomType(String id) {
		return RandomType.values()[getRuleObj(id).getInt("r_type")];
	}

	public static final boolean isCountRandom(String id) {
		return getRuleObj(id).getInt("c_type") == 2;
	}

	public static final int getCountAsInt(String id) {
		return getRuleObj(id).getInt("r_count");
	}

	public static final List<Pair<Integer, Number>> getCountAsPairList(String id) {
		List<Pair<Integer, Number>> ref = new ArrayList<>();
		ISFSArray arr = getRuleObj(id).getSFSArray("r_count");
		for (int i = 0; i < arr.size(); ++i) {
			ISFSObject obj = arr.getSFSObject(i);
			ref.add(Pair.of(obj.getInt("c"), obj.getInt("w")));
		}
		return ref;
	}
}
